package org.example.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.ResultCode.JwtUtils;
import org.example.ResultCode.R;
import org.example.eduorder.entity.Order;
import org.example.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author lining
 * @since 2023-11-09
 */
@CrossOrigin
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    //1.生成订单
    @PostMapping("createOder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request){
        //创建订单,返回订单号
        String orderNo=orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));//获得jwt令牌的成员
        return  R.ok().data("orderId",orderNo);
    }
    //2.根据订单号查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order=orderService.getOne(wrapper);
        return  R.ok().data("item",order);
    }
//    3.根据课程id和用户id查询订单表status值是否为1(已支付)
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count=orderService.count(wrapper);
        if(count>0){
            return  true;
        }
        return  false;
    }

}

